package com.zzu.chat.task;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.zzu.chat.R;
import com.zzu.chat.bean.Task;

import java.util.ArrayList;
import java.util.List;

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
    private List<Task> tasks = new ArrayList<>();

    static class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        ImageButton selectButton;
        TextView startTimeTV;
        TextView endTimeTV;
        TextView content;
        TextView label;
        TextView priority;

        public ViewHolder(View view){
            super(view);
            cardView = view.findViewById(R.id.card_view_time_list);
            selectButton = view.findViewById(R.id.time_list_button);
            startTimeTV= view.findViewById(R.id.time_list_finished_time);
            endTimeTV= view.findViewById(R.id.task_time_list_end_time);
            content= view.findViewById(R.id.time_list_content);
            label= view.findViewById(R.id.task_label);
            priority= view.findViewById(R.id.task_priority);
        }

    }

    public TaskAdapter(List<Task> tasks){
        this.tasks = tasks;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_task_timelist, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Task task = tasks.get(position);
        holder.content.setText(task.getTaskContent());
        holder.startTimeTV.setText(task.getFinishedTime());
        holder.endTimeTV.setText(task.getEndTime());
        holder.label.setText(task.getLabel());
        if (task.isFinished() == true) {
            holder.selectButton.setImageResource(R.drawable.ic_task_time_list_done);
        } else {
            holder.selectButton.setImageResource(R.drawable.ic_add_timelist_button);
        }

        int priority = task.getPriority();
        if (priority == 4) {
            holder.priority.setText("!!!高优先级");
        } else if (priority == 3) {
            holder.priority.setText("!!中优先级");
        } else if (priority == 2) {
            holder.priority.setText("!低优先级");
        } else {
            holder.priority.setText("默认优先级");
        }
    }

    @Override
    public int getItemCount() {
        return tasks.size();
    }


}
